﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;
using UMC.Web.UI;

namespace UMC.Wiki.Activities
{
    class SubjectPortfolioActivity : WebActivity
    {
        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var key = this.AsyncDialog("Key", g => this.DialogValue("EDITER"));
            var user = this.Context.Token.Identity();
            var sid = this.AsyncDialog("Id", d =>
            {
                var ProjectId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Project", g =>
                {
                    UITitle title = UITitle.Create();

                    title.Title = "我的专栏项目";

                    var ui = UISection.Create(title);
                    var ids = new List<ulong>();
                    var pros = new List<Project>();
                    UMC.Data.Utility.Each(DataFactory.Instance().Projects(user.Id.Value, WebAuthType.Admin, WebAuthType.User), dr =>
                    {
                        pros.Add(dr); ids.Add(dr.Id.Value);
                    });

                    if (ids.Count > 0)
                    {
                        var webr = UMC.Web.WebResource.Instance();

                        foreach (var p in pros)
                        {
                            var desc = new UIIconNameDesc(new UIIconNameDesc.Item(webr.ImageResolve(request.Url, p.Id.Value, "1", "4"), p.Caption,
                                String.Format("知识{0}篇", p.SubjectTotal ?? 0))
                                .Click(Web.UIClick.Query(new WebMeta().Put("Project", p.Id))));
                            if (p.user_id == user.Id)
                            {
                                desc.Button("我的");
                            }
                            ui.Add(desc);

                        }
                    }
                    else
                    {
                        ui.Add("Desc", new UMC.Web.WebMeta().Put("desc", "你未有专栏的项目").Put("icon", "\uF016"), new UMC.Web.WebMeta().Put("desc", "{icon}\n{desc}"),

                            new UIStyle().Align(1).Color(0xaaa).Padding(20, 20).BgColor(0xfff).Size(12).Name("icon", new UIStyle().Font("wdk").Size(60)));

                    }
                    ui.SendTo("UI.Event", this.Context, "Subject.Project");
                }));// ?? Guid.Empty;
                if (ProjectId.HasValue == false)
                {
                    this.Prompt("未传入项目");
                }
                var projectItemId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Item", g => this.DialogValue("Item")), 0);// ?? Guid.Empty;

               

                UITitle uITItle = UITitle.Create();
                if (projectItemId == 0)
                {
                    uITItle.Title = "选择栏位";
                }
                else
                {

                    uITItle.Title = "选择目录";
                }

                var team = DataFactory.Instance().Project(ProjectId.Value);
                var sestion = UISection.Create(uITItle);
                if (projectItemId == 0)
                {
                    sestion.AddCell("所属项目", team.Caption);
                    var ui2 = sestion.NewSection();
                    UMC.Data.Utility.Each(DataFactory.Instance().ProjectItem(team), dr =>
                      {

                          ui2.AddCell(dr.Caption, Web.UIClick.Query(new WebMeta().Put("Item", dr.Id)));

                      });
                }
                else
                {
                    sestion.AddCell("所属项目", team.Caption);

                    var project = DataFactory.Instance().ProjectItem(projectItemId);
                    
                    sestion.AddCell("所属栏位", project.Caption);
                    var ui2 = sestion.NewSection();
                    UMC.Data.Utility.Each(DataFactory.Instance().Portfolio(new ProjectItem { Id = project.Id }), dr =>
                    {
                        ui2.AddCell(dr.Caption, new Web.UIClick(request, d, dr.Id.ToString()));
                    });
                }

                sestion.SendTo("UI.Event", this.Context);
            });
            var cmdId = UMC.Data.Utility.UInt64Parse(sid) ?? 0;

            var category = DataFactory.Instance().Portfolio(cmdId) ?? new Portfolio();
            
            switch (key)
            {
                case "EDITER":
                    break;
                default:
                    this.Context.UIEvent(key, new ListItem(category.Caption, category.Id.ToString()), true);
                    break;
            }

            var Caption = Web.UIDialog.AsyncDialog(this.Context, "Caption", d =>
             {

                 var fmdg = new Web.UIFormDialog();
                 fmdg.Title = category.Id.HasValue ? "编辑" : "新建";
                 fmdg.AddText("文集名称", "Caption", category.Caption);
                 fmdg.Submit("确认提交", "Subject.Portfolio", "Subject.Portfolio.Add");
                 return fmdg;

             });
            category.Caption = Caption;

            if (category.Id.HasValue == false)
            {
                var ItemId = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("ItemId", g => this.DialogValue("Item"))).Value;

                var projectItem = DataFactory.Instance().ProjectItem(ItemId);

                var member = DataFactory.Instance().ProjectMember(projectItem.project_id.Value, user.Id.Value);

                if (member != null)
                {
                    switch (member.AuthType)
                    {
                        case WebAuthType.Admin:
                        case WebAuthType.User:
                            break;
                        default:
                            this.Prompt("您未有新增文集的权限");
                            break;
                    }

                }
                else
                {
                    this.Prompt("您未有新增文集的权限");
                }

                category.Id = UMC.Data.Utility.NewId();
                category.project_id = projectItem.project_id;
                category.project_item_id = projectItem.Id;

                category.user_id = user.Id;
                category.CreationTime = DateTime.Now;
                category.SubjectTotal = 0;
                category.Sequence = UMC.Data.Utility.TimeID();


                DataFactory.Instance().Put(category);

                this.Context.Send(new UMC.Web.WebMeta().Put("type", "Subject.Portfolio.Add").Put("Id", category.Id).Put("Text", category.Caption), true);
            }
            else
            {
                DataFactory.Instance().Put(new Portfolio { Caption = Caption, Id = cmdId });
                this.Context.Send(new UMC.Web.WebMeta().Put("type", "Subject.Portfolio").Put("Id", category.Id), true);
            }


        }

    }
}